package com.joker.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.joker.pojo.Messages.FriendMessage;
import com.joker.pojo.Messages.PublicMessage;
import com.joker.pojo.User;
import com.joker.pojo.UserFried;
import com.joker.pojo.UseridandFriendid;
import com.joker.service.UserFriendService;
import com.joker.service.UserFriendService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

//查询好友列表的接口
@Controller
@RequestMapping(value = "/userfriend",produces = "text/json;charset=UTF-8")
public class UserFriendController{

    @Autowired
    @Qualifier("UserFriendServiceImpl")
    private UserFriendService userFriendService;
    @Autowired
    @Qualifier("UserFriendServiceImpl2")
    private UserFriendService2 userFriendService2;

    //查看好友列表(详细)
    @RequestMapping("/list")
    @ResponseBody
    public String showfriendslist(@RequestParam("userid") String userid){
//        先查询一下好友的id列表
        ArrayList useridlist= userFriendService.selectallmyfriendid(Integer.valueOf(userid));
        System.out.println("输出useridlist"+useridlist);
        for (Object friendid:useridlist) {
            System.out.println("输出friendid");
            System.out.println(friendid);
        }
        ArrayList<User> Userlist=new ArrayList<>();
        for (Object user:useridlist) {
            Integer user1= (Integer) user;
            User tempuser=userFriendService2.showmyfiends(user1);
            Userlist.add((tempuser));
        }
        System.out.println("输出查询的好友列表");
        System.out.println(Userlist);
        String userlist= JSON.toJSONString(Userlist);
        return userlist;

    }
//    修改好友申请通知
    @RequestMapping("/updatefriendid")
    @ResponseBody
    public String updatefriendid(@RequestParam("userid") String userid,@RequestParam("friendid") String Friendid,@RequestParam("ifriend") String iffriend ){
        UserFried userFried=new UserFried();
        //通过自己这边的验证
        userFried.setUserid(Integer.parseInt(userid));
        userFried.setFriendid(Integer.parseInt(Friendid));
        userFried.setIffriend(Integer.parseInt(iffriend));

        Integer rest=userFriendService.updatefriendid(userFried);
        //通过对方那边的验证
        userFried.setUserid(Integer.parseInt(Friendid));
        userFried.setFriendid(Integer.parseInt(userid));
        userFried.setIffriend(Integer.parseInt(iffriend));
        Integer rest1=userFriendService.updatefriendid(userFried);

        PublicMessage message=new PublicMessage();
        message.setCode("200");
        if(rest>0){
            message.setMessage("通过好友验证");
        }
        else {
            message.setMessage("添加失败");
        }
        String jt=JSONObject.toJSONString(message);
        return jt;
    }
    @RequestMapping("/usertest")
    @ResponseBody
    public String test()throws Exception{
        System.out.println("一个测试的控制器");
        User tempuser=userFriendService2.showmyfiends(Integer.parseInt("123456"));
        System.out.println(tempuser);
        return "hhh";
    }
    //获取聊天记录
    @RequestMapping("/friendmessage")
    @ResponseBody
    public String fiendmessage(@RequestParam("userid") String userid,@RequestParam("friendid") String friendid){
        UseridandFriendid useridandFriendid=new UseridandFriendid();
        useridandFriendid.setUserid(Integer.parseInt(userid));
        useridandFriendid.setBecalluserid(Integer.parseInt(friendid));
        //查询我给好友发的消息
        ArrayList<FriendMessage> mymessage=userFriendService2.selectfriendmessages(useridandFriendid);
        useridandFriendid.setUserid(Integer.parseInt(friendid));
        useridandFriendid.setBecalluserid(Integer.parseInt(userid));
//        遍历集合
//        int i=0;
//        Iterator iterator=mymessage.iterator();
//        while(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
        ArrayList<FriendMessage> othermessage=userFriendService2.selectfriendmessages(useridandFriendid);
        Iterator iterator=othermessage.iterator();
        while(iterator.hasNext()){
            FriendMessage tempmessage= (FriendMessage) iterator.next();
            tempmessage.setMymess(false);
        }

        Date date;
        //注意format的格式要与日期String的格式相匹配
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TreeMap<Date,FriendMessage>map=new TreeMap<>();
        //先遍历我发的消息
        for (FriendMessage tempmess:mymessage) {
            try {
                date = sdf.parse(tempmess.date);
                map.put(date,tempmess);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (FriendMessage tempmess:othermessage) {
            try{
                date=sdf.parse(tempmess.date);
                map.put(date,tempmess);
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
        //
        List<FriendMessage> messages = new ArrayList<>();
        Set<Date> keySet = map.keySet();
        Iterator<Date> iter = keySet.iterator();
        //Iterator<String> iter = map.keySet().iterator();
        while(iter.hasNext())
        {
            Date str=iter.next();
//            System.out.println(str+"="+map.get(str));
            messages.add(map.get(str));
//            System.out.println(map.get(str));
        }

        String turn=JSON.toJSONString(messages);

        return turn;

    }
}
